using ERP.Domain.Products;
using Product.API.ISerivces.DTOS;
using Product.API.ISerivces.ProductProcess;
using Repository.communal;

namespace Product.API.Serivces.ProductProcess
{
    /// <summary>
    /// 产品工艺服务实现类
    /// </summary>
    public class ProductProcessSerivce : IProductProcessSerivce
    {
        /// <summary>
        /// 产品工艺仓储
        /// </summary>
        private readonly IBaseRepository<ERP.Domain.Products.ProductProcess> productProcessRepository;

        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="productProcessRepository">产品工艺仓储</param>
        public ProductProcessSerivce(IBaseRepository<ERP.Domain.Products.ProductProcess> productProcessRepository)
        {
            this.productProcessRepository = productProcessRepository;
        }


        /// <summary>
        /// 产品工艺批量添加
        /// </summary>
        /// <param name="entity">产品集合</param>
        /// <param name="productRouteNumber">工艺编号</param>
        /// <returns>返回受影响行数</returns>
        public async Task<int> BatchProductProcess(List<BatchProductProcessDTO> entity, Guid productRouteNumber)
        {
            var data = entity.Select(item => new ERP.Domain.Products.ProductProcess
            {
                ProcessRouteNumber = productRouteNumber,
                ProductNumber = item.ProductNumber
            }).ToList();

            int result = await productProcessRepository.BatchAdd(data);
            return result;
        }

        /// <summary>
        /// 产品工艺添加
        /// </summary>
        /// <param name="productProcess">产品工艺信息</param>
        /// <returns>返回受影响行数</returns>
        public async Task<int> CreateProductProcess(ERP.Domain.Products.ProductProcess productProcess)
        {
            var result = await productProcessRepository.AddAsync(productProcess);
            return result;
        }

        /// <summary>
        /// 产品工艺删除（逻辑）
        /// </summary>
        /// <param name="Id">主键</param>
        /// <returns>返回受影响行数</returns>
        public async Task<int> DeleteProductProcess(int Id)
        {
            var data = await productProcessRepository.GetModelAsync(Id);
            data.IsDelete = true;
            int result = await productProcessRepository.Update(data);
            return result;
        }

        /// <summary>
        /// 产品工艺修改
        /// </summary>
        /// <param name="productProcess">产品工艺信息</param>
        /// <returns>返回受影响行数</returns>
        public async Task<int> UpdateProductProcess(ERP.Domain.Products.ProductProcess productProcess)
        {
            var result = await productProcessRepository.Update(productProcess);
            return result;
        }

        /// <summary>
        /// 产品工艺批量修改
        /// </summary>
        /// <param name="entity">产品工艺集合</param>
        /// <param name="processRouteNumber">工艺流程编号</param>
        /// <returns>返回受影响行数</returns>
        public async Task<int> BatchUpdateProductProcess(List<BatchUpdateProductProcessDTO> entity, Guid processRouteNumber)
        {
            var data = entity.Select(item => new ERP.Domain.Products.ProductProcess
            {
                Id = item.Id,
                ProcessRouteNumber = processRouteNumber,
                ProductNumber = item.ProductNumber
            }).ToList();

            var result = await productProcessRepository.BatchUpdate(data);
            return result;
        }
    }
}


